package datastructure.set.set;

import datastructure.tree.BinaryTree;
import datastructure.tree.RedBlackTree;

import java.util.Comparator;

/**
 * @Description: 红黑树实现集合
 * 缺点：元素必须具备可比较性
 * @Author Ammar
 * @Create 2023/4/19 23:33
 */
public class TreeSet<E> implements Set<E> {
    private RedBlackTree<E> tree = new RedBlackTree<>();

    public TreeSet() {
    }

    public TreeSet(Comparator<E> comparator) {
        tree = new RedBlackTree<>(comparator);
    }

    @Override
    public int size() {
        return tree.size();
    }

    @Override
    public boolean isEmpty() {
        return tree.isEmpty();
    }

    @Override
    public void clear() {
        tree.clear();
    }

    @Override
    public boolean contains(E element) {
        return tree.contains(element);
    }

    @Override
    public void add(E element) {
        tree.add(element);
    }

    @Override
    public void remove(E element) {
        tree.remove(element);
    }

    @Override
    public void traversal(Visitor<E> visitor) {
        if (visitor == null) return;
        tree.inorderTraversal(new BinaryTree.Visitor<E>() {
            @Override
            public boolean visit(E element) {
                return visitor.visit(element);
            }
        });
    }
}
